package com.org.khtn.command.select;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.Map.Entry;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.org.khtn.data.Data;
import com.org.khtn.data.DataTables;
import com.org.khtn.ultilities.Ultilities;

public class GroupBy extends Command {
	final String className = "GroupBy:";

	public GroupBy() {
		resultData = new HashMap<String, Multimap<String, String>>();
		data = new HashMap<String, Map<String, Map<String, String>>>();

	}

	public Select groupBy(String fields) {
		String methodName = "groupBy():";
		/*
		 * String[] listFieldTable = fields.split(","); String[] fieldName = new
		 * String[listFieldTable.length]; int[] indexDatabase = new
		 * int[listFieldTable.length]; for (int i=0; i<listFieldTable.length;
		 * i++) { String[] fieldTable = listFieldTable[i].split("."); if
		 * (fieldTable.length > 2 || fieldTable.length == 0) { return null; }
		 * else if (fieldTable.length == 2) { if (tableExist(fieldTable[0]) ==
		 * false) { return null; } else { if (ambiguousField(fieldTable[1])) {
		 * fieldName[i] = fieldTable[1]; indexDatabase[i] =
		 * currentTable.indexOf(fieldTable[0]); } else { fieldName[i] =
		 * fieldTable[1]; indexDatabase[i] = 0; } } } else if (fieldTable.length
		 * == 1) { if (ambiguousField(fieldTable[0])) { return null; } else {
		 * fieldName[i] = fieldTable[0]; indexDatabase[i] = 0; } } }
		 */
		Select select = new Select();
		select.putData(resultData);
		select.setTables(tables);
		select.setData(data);
		select.putAllCurrentTable(currentTable);
		select.setTableDefine(tableDefine);
		select.databaseName =databaseName;
		System.out.println(className + methodName + "fields:" + fields);
		if (fields == null || resultData.containsKey("error")) {

			select.putData(resultData);
			select.setTables(tables);
			select.setData(data);
			select.putAllCurrentTable(currentTable);
			select.setTableDefine(tableDefine);
			select.databaseName =databaseName;
			
			return select;

		}

		String[] listFieldTable = fields.split(",");
		Vector fieldName = new Vector();
		int[] indexTable = new int[listFieldTable.length];
		for (int i = 0; i < listFieldTable.length; i++) {
			String[] fieldTable = listFieldTable[i].split("\\.");
			if (fieldTable.length > 2 || fieldTable.length == 0) {
				return null;
			} else if (fieldTable.length == 2) {
				if (tableExist(fieldTable[0]) == false) {
					{
						resultData = Ultilities.createError("table not exist");
						select.putData(resultData);
						return select;
					}
				} else {
					if (ambiguousField(fieldTable[1])) {
						fieldName.add(fieldTable[1]);
						indexTable[i] = currentTable.indexOf(fieldTable[0]);
					} else {
						fieldName.add(fieldTable[1]);
						indexTable[i] = 0;
					}
				}
			} else if (fieldTable.length == 1) {
				if (ambiguousField(fieldTable[0])) {
					{
						resultData = Ultilities.createError("ambiguous field");
						select.putData(resultData);
						return select;
					}
				} else {
					
					fieldName.add(fieldTable[0]);
					indexTable[i] = 0;
				}
			}
			
		}

		Map<String, Multimap<String, String>> result = new HashMap<String, Multimap<String, String>>();

		/*
		 * for (Map.Entry<String, Multimap<String, String>> data1 : resultData
		 * .entrySet()) { String recordNumber1 = data1.getKey();
		 * Multimap<String, String> recordValues1 = data1.getValue(); Vector
		 * record1 = new Vector(); for (Entry<String, Collection<String>> entry
		 * : recordValues1 .asMap().entrySet()) { String att = (String)
		 * entry.getKey(); List<String> value = new ArrayList<String>();
		 * value.addAll(entry.getValue()); if (fieldName.contains(att)) { int
		 * index = indexTable[fieldName.indexOf(att)];
		 * record1.add(value.get(index)); } }
		 * 
		 * for (Map.Entry<String, Multimap<String, String>> data2 : resultData
		 * .entrySet()) { String recordNumber2 = data2.getKey();
		 * Multimap<String, String> recordValues2 = data2.getValue(); Vector
		 * record2 = new Vector(); if
		 * (recordNumber1.compareToIgnoreCase(recordNumber2) != 0) { for
		 * (Entry<String, Collection<String>> entry : recordValues2
		 * .asMap().entrySet()) { String att = (String) entry.getKey();
		 * List<String> value = new ArrayList<String>();
		 * value.addAll(entry.getValue()); if (fieldName.contains(att)) { int
		 * index = indexTable[fieldName.indexOf(att)];
		 * record2.add(value.get(index)); } } }
		 * 
		 * } }
		 */

		Vector resultValue = new Vector();
		Vector resultKey = new Vector();

		for (Map.Entry<String, Multimap<String, String>> data1 : resultData
				.entrySet()) {
			String recordNumber1 = data1.getKey();
			Multimap<String, String> recordValues1 = data1.getValue();
			Vector record1 = new Vector();
			for (Entry<String, Collection<String>> entry : recordValues1
					.asMap().entrySet()) {
				String att = (String) entry.getKey();
				List<String> value = new ArrayList<String>();
				value.addAll(entry.getValue());
				if (fieldName.contains(att)) {
					int index = indexTable[fieldName.indexOf(att)];
					record1.add(value.get(index));
				}
			}
			if (resultValue.contains(record1) == false) {
				resultValue.add(record1);
				resultKey.add(recordNumber1);
			}
		}

		for (int i = 0; i < resultValue.size(); i++) {
			Vector record = (Vector) resultValue.elementAt(i);
			Multimap<String, String> tam = ArrayListMultimap.create();
			for (int j = 0; j < fieldName.size(); j++) {
				tam.put((String) fieldName.elementAt(j),
						(String) record.elementAt(j));
			}
			result.put((String) resultKey.elementAt(i), tam);
		}

		select.putData(result);
		select.setTables(tables);
		select.setData(data);
		select.putAllCurrentTable(currentTable);
		select.setTableDefine(tableDefine);
		select.databaseName =databaseName;
		
		return select;
	}
}
